<?php

namespace Admin;

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */

/**
 * Description of GroupsController
 *
 * @author dung
 */
use AdminController,
    Str,
    View,
    Input,
    Sentry,
    Config,
    Redirect,
    Validator,
    Lang,
    Category;

class GroupsController extends AdminController
{

    /**
     * Show a list of all the groups.
     *
     * @return View
     */
    public function getIndex()
    {
        // Grab all the groups
        $groups = Sentry::getGroupProvider()->createModel()->paginate();

        // Show the page
        return View::make('backend/groups/index', compact('groups'));
    }

    /**
     * Group create.
     *
     * @return View
     */
    public function getCreate()
    {
        if (!Sentry::getUser()->hasAnyAccess(['group', 'group.create']))
            return View::make('backend/notallow');
        // Get all the available permissions
        $permissions = Config::get('permissions');
        $this->encodeAllPermissions($permissions, true);

        // Selected permissions
        $selectedPermissions = Input::old('permissions', array());

        // Show the page
        return View::make('backend/groups/create', compact('permissions', 'selectedPermissions'));
    }

    /**
     * Group create form processing.
     *
     * @return Redirect
     */
    public function postCreate()
    {
        if (!Sentry::getUser()->hasAnyAccess(['group', 'group.create']))
            return View::make('backend/notallow');
        // Declare the rules for the form validation
        $rules = array(
            'name' => 'required',
        );

        // Create a new validator instance from our validation rules
        $validator = Validator::make(Input::all(), $rules);

        // If validation fails, we'll exit the operation now.
        if ($validator->fails()) {
            // Ooops.. something went wrong
            return Redirect::back()->withInput()->withErrors($validator);
        }

        try {
            // We need to reverse the UI specific logic for our
            // permissions here before we create the user.
            $permissions = Input::get('permissions', array());
            $this->decodePermissions($permissions);
            app('request')->request->set('permissions', $permissions);

            // Get the inputs, with some exceptions
            $inputs = Input::only('name', 'permissions');

            // Was the group created?
            if ($group = Sentry::getGroupProvider()->create($inputs)) {
                // Redirect to the new group page
                return Redirect::route('update/group', $group->id)->with('success', Lang::get('admin/groups/message.success.create'));
            }

            // Redirect to the new group page
            return Redirect::route('create/group')->with('error', Lang::get('admin/groups/message.error.create'));
        } catch (NameRequiredException $e) {
            $error = 'group_name_required';
        } catch (GroupExistsException $e) {
            $error = 'group_exists';
        }

        // Redirect to the group create page
        return Redirect::route('create/group')->withInput()->with('error', Lang::get('admin/groups/message.' . $error));
    }

    /**
     * Group update.
     *
     * @param  int $id
     * @return View
     */
    public function getEdit($id = null)
    {
        if (!Sentry::getUser()->hasAnyAccess(['group', 'group.edit']))
            return View::make('backend/notallow');

        try {
            // Get the group information
            $group = Sentry::getGroupProvider()->findById($id);

            // Get all the available permissions
            $permissions = Config::get('permissions');

            foreach (Category::where('status', 'on')->where('parent_id', 0)->get() as $cat) {
                $catPermissions['Phân quyền danh mục'][] = array(
                    'permission' => 'category.c' . $cat->id,
                    'label' => $cat->name,
                    'can_inherit' => 1,
                    'type' => 'cat');
            }
            $this->encodeAllPermissions($catPermissions, true);

            $this->encodeAllPermissions($permissions, true);
            // Get this group permissions
            $groupPermissions = $group->getPermissions();

            $this->encodePermissions($groupPermissions);
            $groupPermissions = array_merge($groupPermissions, Input::old('permissions', array()));
        } catch (GroupNotFoundException $e) {
            // Redirect to the groups management page
            return Redirect::route('groups')->with('error', Lang::get('admin/groups/message.group_not_found', compact('id')));
        }
        // Show the page
        return View::make('backend/groups/edit', compact('group', 'permissions', 'catPermissions', 'groupPermissions'));
    }

    /**
     * Group update form processing page.
     *
     * @param  int $id
     * @return Redirect
     */
    public function postEdit($id = null)
    {
        if (!Sentry::getUser()->hasAnyAccess(['group', 'group.edit']))
            return View::make('backend/notallow');

        // We need to reverse the UI specific logic for our
        // permissions here before we update the group.
        $permissions = Input::get('permissions', array());
        $cpermissions = Input::get('cpermissions', array());
        $this->decodePermissions($permissions);

        $permissions = array_merge($cpermissions, $permissions);
        // print_r($permissions); die();
        app('request')->request->set('permissions', $permissions);

        try {
            // Get the group information
            $group = Sentry::getGroupProvider()->findById($id);
        } catch (GroupNotFoundException $e) {
            // Redirect to the groups management page
            return Rediret::route('groups')->with('error', Lang::get('admin/groups/message.group_not_found', compact('id')));
        }

        // Declare the rules for the form validation
        $rules = array(
            'name' => 'required',
        );

        // Create a new validator instance from our validation rules
        $validator = Validator::make(Input::all(), $rules);

        // If validation fails, we'll exit the operation now.
        if ($validator->fails()) {
            // Ooops.. something went wrong
            return Redirect::back()->withInput()->withErrors($validator);
        }

        try {
            // Update the group data
            $group->name = Input::get('name');
            $group->permissions = Input::get('permissions');
            // print_r($group); die();
            // Was the group updated?
            if ($group->save()) {
                // Redirect to the group page
                return Redirect::route('update/group', $id)->with('success', Lang::get('admin/groups/message.success.update'));
            } else {
                // Redirect to the group page
                return Redirect::route('update/group', $id)->with('error', Lang::get('admin/groups/message.error.update'));
            }
        } catch (NameRequiredException $e) {
            $error = Lang::get('admin/group/message.group_name_required');
        }

        // Redirect to the group page
        return Redirect::route('update/group', $id)->withInput()->with('error', $error);
    }

    /**
     * Delete the given group.
     *
     * @param  int $id
     * @return Redirect
     */
    public function getDelete($id = null)
    {
        if (!Sentry::getUser()->hasAnyAccess(['group', 'group.delete']))
            return View::make('backend/notallow');

        try {
            // Get group information
            $group = Sentry::getGroupProvider()->findById($id);

            // Delete the group
            $group->delete();

            // Redirect to the group management page
            return Redirect::route('groups')->with('success', Lang::get('admin/groups/message.success.delete'));
        } catch (GroupNotFoundException $e) {
            // Redirect to the group management page
            return Redirect::route('groups')->with('error', Lang::get('admin/groups/message.group_not_found', compact('id')));
        }
    }

}
